package com.mhy.hylogcat;

import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.mhy.logcat.LogCat;

import java.io.File;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class MainActivity extends AppCompatActivity {
    private int anInt = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = findViewById(R.id.text);
        textView.setMovementMethod(ScrollingMovementMethod.getInstance());
        textView.setVerticalScrollBarEnabled(true);
        Button button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("");
            }
        });


        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                anInt++;
                String msg = "Hello World"+anInt;
                LogCat.i("onClick:", msg);
                LogCat.d(msg);
                textView.append(msg+"\r");
            }
        });
        TextView tv = findViewById(R.id.textView);
        String ip = getIpAddress();
        tv.setText("我的ip:"+ip);

        LogCat.enableLogPrint(true);
        // 这里设置发送日志可以在设置页填入ip和端口，可动态修改和关闭
        // TODO 这里为了方便用的广播，实际使用自己服务端单播ip
        String udpIp = ip.substring(0, ip.lastIndexOf("."))+".255";
        LogCat.d("udpIp: " + udpIp);
        LogCat.enableLogSend(true);
        //udp/file 二选一
        LogCat.setSendLog(udpIp, 9696);
//        LogCat.setFileLog(new File(getExternalFilesDir("log"), "log_"+ System.currentTimeMillis() +".txt"));
    }

    static String getIpAddress() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            if (networkInterfaces == null) {
                return "0.0.0.0";
            }
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            LogCat.e(e);
            return "0.0.0.0";
        }
        return "0.0.0.0";
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}